package com.shouhuyun.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.shouhuyun.constant.MessageConstant;
import com.shouhuyun.constant.RedisCheckConstant;
import com.shouhuyun.constant.RedisConstant;
import com.shouhuyun.entity.PageResult;
import com.shouhuyun.entity.QueryPageBean;
import com.shouhuyun.entity.Result;
import com.shouhuyun.pojo.CheckGroup;
import com.shouhuyun.pojo.Setmeal;
import com.shouhuyun.service.SetmealService;
import com.shouhuyun.utils.QiniuUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import javax.xml.ws.handler.MessageContext;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 体检套餐
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private RedisController redisController;
    @Reference
    private SetmealService setmealService;
//    使用JedisPool操作Redis库
    @Autowired
    private JedisPool jedisPool;
//    文件上传
    //注意spring-mvc里要配置文件上传组件
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
//        测试 文件是否能 传到后台
//        System.out.println(imgFile);
//          获取原始文件后缀
        String originalFilename = imgFile.getOriginalFilename();  //原始文件名
        int index = originalFilename.lastIndexOf(".");
        String extention = originalFilename.substring(index); //  .jpg
//        通过UUID  获取一个36位的随机名
        String fileName = UUID.randomUUID().toString() + extention;

        try {
//          通过七牛云传到七牛云服务器
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
//          将上传图片名称存入Redis，基于Redis的Set集合存储
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
        return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
    }

//    新增套餐
    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        try {
//        传过来  检查组的id  以及  套餐的基本信息
            setmealService.add(setmeal, checkgroupIds);

            //将操作添加到redis中
            redisController.saveRedis(RedisCheckConstant.SETMEAL_ADD);
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }

    }

    @RequestMapping("/findPage")
    //分页查询套餐
//    接收  查询对象 queryPageBean  里面是 当前页 页面大小  查询条件  三个参数
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        //将操作添加到redis中
        String s = queryPageBean.getQueryString();
        if (s != null && s != "")
            redisController.saveRedis(RedisCheckConstant.SETMEAL_QUERY);

//        调用pagequery方法
        return setmealService.Pagequery(queryPageBean);
    }

    // 点击编辑时 根据套餐的id  查询套餐信息用于基本信息回显
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Setmeal setmeal = setmealService.findById(id);
//        查询套餐成功
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        } catch (Exception e) {
            e.printStackTrace();
//            查询套餐失败
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

//   点击编辑时 查询套餐下的  所有检查组信息
    @RequestMapping("/findCheckGroupIdsBySetmealId")
    public Result findCheckGroupIdsBySetmealId(Integer id){
        try {
            List<Integer> setmealIds = setmealService.findCheckGroupIdsBySetmealId(id);
//        查询套餐相关检查组成功
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,setmealIds);
        } catch (Exception e) {
            e.printStackTrace();
//            查询套餐相关检查组失败
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

    }

    //    编辑完成后 点击确定 保存数据
    @RequestMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        try {

            setmealService.edit(setmeal, checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
//            编辑失败
            return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL);
        }

        //将操作添加到redis中
        redisController.saveRedis(RedisCheckConstant.SETMEAL_EDIT);

//        编辑成功
        return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
    }

    //    删除套餐
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            setmealService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.DELETE_SETMEAL_FAIL);
        }

        //将操作添加到redis中
        redisController.saveRedis(RedisCheckConstant.SETMEAL_DELETE);

        return  new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
    }


}


